Quiz 1 (Week 1)
Hint: some of these questions can be a bit subtle, and will require you to think it through. Don't rush it, and don't try it without tool support. You should make full use of the GHCi Haskell interpreter, and Hoogle, as appropriate.
1 Circularity
1.1 Question 1
What is the answer to this question?
Hint: the answer to this question can be found on the Ed forums. If you're not on the forums already, you should be; it's the place for discussion and announcements. See instructions on the home page if you're having trouble accessing them.
- Catsmeat Potter-Pirbright
- Roderick Spode
- Gussie Fink-Nottle
- Madeline Bassett
2 Typing
In what follows, assume for the sake of simplicity that
all numeric literals have type Int
. Determine the type of the following Haskell expressions!
2.1 Question 2
"hello world"
char*
[Char]
string
[String]
2.2 Question 3
[('a',5,6),('b',7,8)]
(Char, Int, Int)
[(Char,Int,Int)]
([Char,Int,Int])
([Char],[Int],[Int])
2.3 Question 4
snd ("hello", ("world":[]))
[[Char]]
[[[Char]]]
[Char]
String
2.4 Question 5
map (\x -> x + 1)
[a] -> [b]
Int -> Int
[Int] -> [Int]
(Int -> Int) -> [Int] -> [Int]
- Invalid, as not enough arguments are given to
map
.
3 Parentheses
Choose all equivalent parenthesizations of the given expressions.
3.1 Question 6
reverse "hello" ++ "world"
(reverse "hello") ++ "world"
((reverse "hello") ++ "world")
reverse ("hello" ++ "world")
reverse ("hello" ++) "world"
3.2 Question 7
constant3 :: a -> b -> c -> a
constant3 :: a -> (b -> c -> a)
constant3 :: (a -> b) -> (c -> a)
constant3 :: a -> (b -> (c -> a))
constant3 :: a -> [b -> c -> a]
4 Evaluation
Choose all expressions that are equivalent to the given expressions. By "equivalent", we mean that the expressions evaluate to equal results. We consider two functions equal if, for any input, they produce equal outputs (functional extensionality).
4.1 Question 8
Note: The functions ord
and chr
are from Data.Char
.
They convert Char
values to/from
their ASCII (or unicode) numbers, respectively.
For these questions, the answers may have a more general
type than the original expression. So long as a given answer
has equivalent behaviour for the type of the original expression, we
consider the answer to be equivalent.
let increment x = 1 + x in \xs -> map chr (map increment (map ord xs))
map chr . map (1+) . map ord
map (chr . (1+) . ord)
map succ
map chr $ map (1+) $ map ord
\xs -> map chr . map (1+) $ map ord xs